package com.ruoyi.business.appuser.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.business.appuser.entity.Device;
import com.ruoyi.business.appuser.mapper.DeviceMapper;
import com.ruoyi.business.appuser.service.IDeviceService;
import com.ruoyi.business.appuser.vo.DeviceVo;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author Tellsea
 * @date 2025-05-22
 */
@Service
public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> implements IDeviceService {

    @Override
    public TableDataInfo<DeviceVo> queryList(DeviceVo entity) {
        return PageUtils.buildDataInfo(this.baseMapper.queryList(PageUtils.buildPage(), entity));
    }

    @Override
    public List<DeviceVo> queryAll(DeviceVo entity) {
        return this.baseMapper.queryList(entity);
    }

    @Override
    public DeviceVo queryById(Long deviceId) {
        return this.baseMapper.queryById(deviceId);
    }

    @Override
    public void updateStatus(Device device) {
        this.baseMapper.updateStatus(device);
    }

    @Override
    public List<DeviceVo> queryByDeviceName(String deviceName) {
        List<DeviceVo> collect = null;
        List<Device> list = list(new LambdaQueryWrapper<Device>().eq(Device::getDeviceName, deviceName));
        if (CollectionUtil.isNotEmpty(list)){
             collect = list.stream().map(item -> {
                DeviceVo vo = new DeviceVo();
                BeanUtils.copyProperties(item, vo);
                return vo;
            }).collect(Collectors.toList());
        }
        return collect;
    }
}
